package aip.core.mvc.pageController;

import java.util.Calendar;

import aip.core.mvc.pageException.PageEnumCountException;
import aip.core.mvc.pageException.ShowPageException;

public class PageController
{
    private int rowCount = 0;

    private int pageSize = 0;

    private int showPage = 1;

    private int pageCount = 0;

    private StringBuffer showHtml = new StringBuffer();

    private String url = null;

    private StringBuffer showUrl = new StringBuffer();
    private boolean isShowPageEnum;
    private boolean isShowDataRowCount = true;

    private String inputClassName = null;

    private String buttonClassName = null;

    private int showPageEnumCount = 5;

    private String divClassName = null;

    private boolean isShowInput = false;

    private boolean isShowInput()
    {
        return this.isShowInput;
    }

    public void setShowInput(boolean isShowInput)
    {
        this.isShowInput = isShowInput;
    }

    private String getDivClassName()
    {
        return this.divClassName;
    }

    public void setDivClassName(String divClassName)
    {
        this.divClassName = divClassName;
    }

    private int getShowPageEnumCount()
    {
        return this.showPageEnumCount;
    }

    public void setShowPageEnumCount(int showPageEnumCount)
            throws PageEnumCountException
    {
        if ((showPageEnumCount <= 0) || (showPageEnumCount > 10))
        {
            throw new PageEnumCountException();
        }
        this.showPageEnumCount = showPageEnumCount;
    }

    private boolean isShowPageEnum()
    {
        return this.isShowPageEnum;
    }

    public void setShowPageEnum(boolean isShowPageEnum)
    {
        this.isShowPageEnum = isShowPageEnum;
    }

    private StringBuffer getShowUrl()
    {
        return this.showUrl;
    }

    private String getUrl()
    {
        return this.url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }

    private int getPageCount()
    {
        this.pageCount = ((getRowCount() % this.pageSize == 0) ? getRowCount()
                / this.pageSize : getRowCount() / this.pageSize + 1);
        return this.pageCount;
    }

    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    private int getRowCount()
    {
        return this.rowCount;
    }

    public void setRowCount(int rowCount)
    {
        this.rowCount = rowCount;
    }

    private int getShowPage()
    {
        return this.showPage;
    }

    public void setShowPage(int showPage) throws ShowPageException
    {
        if (showPage >= getPageCount())
        {
            throw new ShowPageException();
        }
        this.showPage = showPage;
    }

    private void setPageEnum()
    {
        if (isShowPageEnum())
        {
            int muen = getShowPage() / getShowPageEnumCount();
            int j = muen * getShowPageEnumCount();
            int i = getShowPageEnumCount();
            if (j != 0)
            {
                i = (muen + 1) * getShowPageEnumCount();
            }

            if (muen != 0)
            {
                this.showHtml.append("&nbsp<a href='" + getShowUrl().toString()
                        + "start=" + (j - getShowPageEnumCount()) + "'><<</a>");
            }
            for (; j < i; ++j)
            {
                if (j == getPageCount()) break;
                if (this.showPage == j)
                    this.showHtml
                            .append("&nbsp<font style='color=red' ><strong>"
                                    + (j + 1) + "</strong></font>");
                else
                {
                    this.showHtml.append("&nbsp<a href='"
                            + getShowUrl().toString() + "start=" + j + "'>["
                            + (j + 1) + "]</a>");
                }

            }

            if (j != getPageCount())
                this.showHtml.append("&nbsp<a href='" + getShowUrl().toString()
                        + "start=" + j + "'>>></a>");
        }
    }

    private void setShowInputHtml()
    {
        Calendar c = Calendar.getInstance();
        int inputName = c.hashCode();
        this.showHtml
                .append("<script language='JavaScript' type='text/javascript'>");
        this.showHtml.append("function isNumber(s)");
        this.showHtml.append("{");
        this.showHtml.append("var digits = '0123456789';");
        this.showHtml.append("var i = 0;");
        this.showHtml.append("var sLength = s.length;");
        this.showHtml.append("while ((i < sLength))");
        this.showHtml.append("{");
        this.showHtml.append("var c = s.charAt(i);");
        this.showHtml.append("if (digits.indexOf(c) == -1) return false;");
        this.showHtml.append("i++;");
        this.showHtml.append("}");
        this.showHtml.append("return true;");
        this.showHtml.append("}");
        this.showHtml.append("function checkNumber(name){");
        this.showHtml.append("var bbb = document.getElementById(name).value;");
        this.showHtml.append("if(isNumber(bbb)){");
        this.showHtml.append("return true;");
        this.showHtml.append("}else{");
        this.showHtml.append("document.getElementById(name).value ='';");
        this.showHtml.append("return false;");
        this.showHtml.append("}");
        this.showHtml.append("}");
        this.showHtml.append("function linkHref(name){");
        this.showHtml.append("if(checkNumber(name)){");
        this.showHtml
                .append("var inputvalue = document.getElementById(name).value;");
        this.showHtml.append("if(inputvalue >=1 && inputvalue <= "
                + this.pageCount + "){");
        this.showHtml.append("window.location.href='" + getShowUrl()
                + "start='+(inputvalue-1)+''");
        this.showHtml.append("}else{");
        this.showHtml.append("alert('没有该页，请确认');");
        this.showHtml.append("document.getElementById(name).value='';");
        this.showHtml.append("document.getElementById(name).focus();");
        this.showHtml.append("}");
        this.showHtml.append("}");
        this.showHtml.append("}");
        this.showHtml.append("</script>");
        this.showHtml.append("&nbsp<input id=" + inputName
                + " type='text' name='" + inputName + "' class="
                + getInputClassName() + " onKeyUp='checkNumber(" + inputName
                + ")'>&nbsp");
        this.showHtml
                .append("<input ID='aaaaa' type='button' value='Go' class="
                        + getButtonClassName() + " onClick='linkHref("
                        + inputName + ")'>&nbsp");
    }

    public String getPageController()
    {
        if (this.showHtml != null)
        {
            this.showHtml.delete(0, this.showHtml.length());
        }

        this.showUrl.append(getUrl());
        if (getUrl().indexOf(63) > -1)
        {
            this.showUrl.append("&");
        }
        else
        {
            this.showUrl.append("?");
        }

        // if (this.rowCount <= this.pageSize)
        // {
        // this.showHtml.append("<table border='0' width='100%'>");
        // this.showHtml.append("<tr>");
        // this.showHtml.append("<td align='right'>");
        // this.showHtml
        // .append("<font style='font-size=12px' color='red'>共有1页</font>");
        // this.showHtml.append("</td>");
        // this.showHtml.append("</tr>");
        // this.showHtml.append("</table>");
        // }

        this.showHtml.append("<div style='font-size=12px' class='"
                + getDivClassName() + "'>");

        if (this.showPage != 0)
        {
            this.showHtml.append("&nbsp<a href='" + getShowUrl().toString()
                    + "start=0' >[首页]</a>");
            this.showHtml.append("&nbsp<a href='" + getShowUrl().toString()
                    + "start=" + (getShowPage() - 1) + "'>上一页</a>");
        }

        setPageEnum();

        if (this.showPage < getPageCount() - 1)
        {
            this.showHtml.append("&nbsp<a href='" + getShowUrl().toString()
                    + "start=" + (this.showPage + 1) + "'>下一页</a>");
            this.showHtml.append("&nbsp<a href='" + getShowUrl().toString()
                    + "start=" + (getPageCount() - 1) + "'>[末页]</a>&nbsp");
        }

        if (isShowInput())
        {
            setShowInputHtml();
        }

        if (isShowDataRowCount())
        {
            this.showHtml
                    .append("&nbsp共<font style='font-size=12px' color='red'><strong>"
                            + getRowCount()
                            + "</strong></font>条记录&nbsp共有<font style='font-size=12px' color='red'><strong>"
                            + getPageCount() + "</strong></font>页");
        }

        this.showHtml.append("</div>");
        return this.showHtml.toString();
    }

    private boolean isShowDataRowCount()
    {
        return this.isShowDataRowCount;
    }

    public void setShowDataRowCount(boolean isShowDataRowCount)
    {
        this.isShowDataRowCount = isShowDataRowCount;
    }

    private String getButtonClassName()
    {
        return this.buttonClassName;
    }

    public void setButtonClassName(String buttonClassName)
    {
        this.buttonClassName = buttonClassName;
    }

    private String getInputClassName()
    {
        return this.inputClassName;
    }

    public void setInputClassName(String inputClassName)
    {
        this.inputClassName = inputClassName;
    }
}
